home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / mallcrawl.swf / scripts / __Packages / Rescuer.as < prev    next >
Encoding:
Text File  |  2007-09-28  |  10.3 KB  |  360 lines

  1. class Rescuer extends MovieClip
  2. {
  3.    var score_readable;
  4.    var direction;
  5.    var life;
  6.    var glevel;
  7.    var char_name;
  8.    var invincible;
  9.    var jump_is_available;
  10.    var is_available;
  11.    var move_speed;
  12.    var ESCALATE_SPEED;
  13.    var accel;
  14.    var movement;
  15.    var BACKGROUND_WIDTH;
  16.    var WIDTH_BOUNDS;
  17.    var Char;
  18.    var lastHitFollower;
  19.    var follower;
  20.    var ground;
  21.    var currentPowerItem;
  22.    var last_escalator;
  23.    var interval_id;
  24.    var Wheeler;
  25.    var Trike;
  26.    var Guitar;
  27.    var SkateBoard;
  28.    var STAGE_WIDTH = 700;
  29.    var STAGE_HEIGHT = 450;
  30.    var JUMP_SPEED = 4.8;
  31.    var DEFAULT_SPEED = 5;
  32.    var GRAVITY = 0.9;
  33.    var ACCEL_ACCEL = 0.2;
  34.    var HEIGHT_MINUS = 16;
  35.    function Rescuer()
  36.    {
  37.       super();
  38.       this._name = "Rescuer";
  39.       _global[this._name] = this;
  40.       this.score_readable = 0;
  41.       this.direction = 0;
  42.       this.life = 1;
  43.       this.glevel = 1;
  44.       this.char_name = _global.char_selected;
  45.       this.gotoAndStop(this.char_name);
  46.       this.resetGround();
  47.       this.invincible = false;
  48.       this.jump_is_available = true;
  49.       this.is_available = true;
  50.       this.move_speed = this.DEFAULT_SPEED;
  51.       this.ESCALATE_SPEED = this.DEFAULT_SPEED / 2;
  52.       this.accel = 0;
  53.       this.movement = [];
  54.       this.accel = 0;
  55.       this.BACKGROUND_WIDTH = _global.MallCrawl._width;
  56.       this.WIDTH_BOUNDS = {left:this.STAGE_WIDTH / 3 + this._width,right:this.STAGE_WIDTH - this.STAGE_WIDTH / 3 - this._width};
  57.       _global.DebugWindow.watchRescuer();
  58.    }
  59.    function onEnterFrame()
  60.    {
  61.       if(this.is_available && !_global.MallCrawl.paused)
  62.       {
  63.          if(Key.isDown(32))
  64.          {
  65.             if(this.jump_is_available)
  66.             {
  67.                this.jump();
  68.             }
  69.          }
  70.          if(Key.isDown(38))
  71.          {
  72.             if(this.hitTest(_global.MallCrawl.Background.Exit))
  73.             {
  74.                if(!_global.MallCrawl.twinsArray.length)
  75.                {
  76.                   _global.MallCrawl.winLevel();
  77.                }
  78.             }
  79.             var _loc3_ = 0;
  80.             while(_loc3_ < _global.MallCrawl.upEscalators.length)
  81.             {
  82.                if(this.hitTest(_global.MallCrawl.upEscalators[_loc3_].HitSquare))
  83.                {
  84.                   this.rideEscalator(_global.MallCrawl.upEscalators[_loc3_],"up");
  85.                }
  86.                _loc3_ = _loc3_ + 1;
  87.             }
  88.          }
  89.          if(Key.isDown(40))
  90.          {
  91.             _loc3_ = 0;
  92.             while(_loc3_ < _global.MallCrawl.downEscalators.length)
  93.             {
  94.                if(this.hitTest(_global.MallCrawl.downEscalators[_loc3_].HitSquare))
  95.                {
  96.                   this.rideEscalator(_global.MallCrawl.downEscalators[_loc3_],"down");
  97.                }
  98.                _loc3_ = _loc3_ + 1;
  99.             }
  100.          }
  101.          if(Key.isDown(39) || Key.isDown(37))
  102.          {
  103.             this.Char.gotoAndStop("walk");
  104.             if(Key.isDown(39))
  105.             {
  106.                this.moveRight();
  107.             }
  108.             if(Key.isDown(37))
  109.             {
  110.                this.moveLeft();
  111.             }
  112.          }
  113.          else
  114.          {
  115.             this.Char.gotoAndStop("still");
  116.             this.direction = 0;
  117.          }
  118.          _loc3_ = 0;
  119.          while(_loc3_ < _global.MallCrawl.twinsArray.length)
  120.          {
  121.             if(this.hitTest(_global.MallCrawl.twinsArray[_loc3_]) && _global.MallCrawl.twinsArray[_loc3_].is_available)
  122.             {
  123.                this.score_readable += _global.Twins.twin_points;
  124.                this.lastHitFollower = _global.MallCrawl.twinsArray[_loc3_];
  125.                if(this.followerCheck(this) != _global.MallCrawl.twinsArray[_loc3_])
  126.                {
  127.                   _global.Footer.addScore(500);
  128.                   if(this.follower)
  129.                   {
  130.                      var _loc4_ = this.followerCheck(this.follower);
  131.                      _loc4_.fillMovement();
  132.                      _loc4_.addFollower(_global.MallCrawl.twinsArray[_loc3_]);
  133.                   }
  134.                   else
  135.                   {
  136.                      this.fillMovement();
  137.                      this.addFollower(_global.MallCrawl.twinsArray[_loc3_]);
  138.                      _global.MallCrawl.twinsArray[_loc3_].onEnterFrame = undefined;
  139.                   }
  140.                }
  141.             }
  142.             _loc3_ = _loc3_ + 1;
  143.          }
  144.       }
  145.       if(this.is_available)
  146.       {
  147.          this._y += this.GRAVITY + this.accel;
  148.          if(this._y + this.accel >= this.ground)
  149.          {
  150.             this._y = this.ground;
  151.             this.jump_is_available = true;
  152.             this.accel = 0;
  153.          }
  154.          else
  155.          {
  156.             this.jump_is_available = false;
  157.             this.accel += this.ACCEL_ACCEL;
  158.          }
  159.       }
  160.       if(this.follower)
  161.       {
  162.          this.movement.push({x:this._x,y:this._y - this.HEIGHT_MINUS,scale:this._xscale,moving:this.Char._currentframe > 1});
  163.          if(this.movement.length > _global.MallCrawl.FOLLOWER_DISTANCE)
  164.          {
  165.             this.follower.twinMovement(this.movement.popFirst());
  166.          }
  167.       }
  168.    }
  169.    function loseFollower()
  170.    {
  171.       this._parent.attachMovie("Damage","Damage",this._parent.getNextHighestDepth(),{_x:this._x,_y:this._y});
  172.       var _loc2_ = this.followerCheck(this);
  173.       if(_loc2_ != this)
  174.       {
  175.          _loc2_.runAway(this.leaderCheck(this,this.followerCheck(this.follower)));
  176.       }
  177.       else
  178.       {
  179.          this.loseLife();
  180.       }
  181.    }
  182.    function loseLife()
  183.    {
  184.       _global.Footer.setLives(_global.Footer.lives - 1);
  185.       if(!_global.Footer.lives)
  186.       {
  187.          _global.MallCrawl.gameOver();
  188.       }
  189.    }
  190.    function fillMovement()
  191.    {
  192.       var _loc3_ = 0;
  193.       while(_loc3_ < _global.MallCrawl.FOLLOWER_DISTANCE)
  194.       {
  195.          this.movement.push({x:this._x,y:this._y - this.HEIGHT_MINUS,scale:this._xscale,moving:this.Char._currentframe > 1});
  196.          if(this.movement.length > _global.MallCrawl.FOLLOWER_DISTANCE)
  197.          {
  198.             this.follower.twinMovement(this.movement.popFirst());
  199.          }
  200.          _loc3_ = _loc3_ + 1;
  201.       }
  202.       this.follower.fillMovement();
  203.    }
  204.    function fadeAlpha(amt)
  205.    {
  206.       this._alpha = amt;
  207.       this.follower.fadeAlpha(this._alpha);
  208.       this.fillMovement();
  209.       return this._alpha;
  210.    }
  211.    function escalate(escalator)
  212.    {
  213.       if(escalator.direction < 0)
  214.       {
  215.          if(this._y + this._height - this.HEIGHT_MINUS > escalator._y)
  216.          {
  217.             this.invincible = true;
  218.             this.moveRight(this.ESCALATE_SPEED);
  219.             this._y -= this.ESCALATE_SPEED;
  220.          }
  221.          else
  222.          {
  223.             if(!this.currentPowerItem)
  224.             {
  225.                this.invincible = false;
  226.             }
  227.             this.is_available = true;
  228.             this.last_escalator = undefined;
  229.             this.resetGround();
  230.             clearInterval(this.interval_id);
  231.          }
  232.       }
  233.       else if(this._y + this._height < escalator._y + escalator._height + this.HEIGHT_MINUS)
  234.       {
  235.          this.invincible = true;
  236.          this.moveRight(this.ESCALATE_SPEED);
  237.          this._y += this.ESCALATE_SPEED;
  238.       }
  239.       else
  240.       {
  241.          if(!this.currentPowerItem)
  242.          {
  243.             this.invincible = false;
  244.          }
  245.          this.is_available = true;
  246.          this.last_escalator = undefined;
  247.          this.resetGround();
  248.          clearInterval(this.interval_id);
  249.       }
  250.    }
  251.    function followerCheck(mc)
  252.    {
  253.       if(mc.follower)
  254.       {
  255.          return this.followerCheck(mc.follower);
  256.       }
  257.       return mc;
  258.    }
  259.    function leaderCheck(mc, name)
  260.    {
  261.       if(mc.follower == name)
  262.       {
  263.          return mc;
  264.       }
  265.       if(mc.follower)
  266.       {
  267.          return this.leaderCheck(mc.follower,name);
  268.       }
  269.       return false;
  270.    }
  271.    function addFollower(mc)
  272.    {
  273.       mc.onEnterFrame = undefined;
  274.       mc.gotoAndStop("walk");
  275.       _global.MallCrawl.tcounterArray.push(mc);
  276.       this.follower = _global.MallCrawl.twinsArray.popByName(mc);
  277.       this.follower.movement = new Array();
  278.    }
  279.    function resetGround()
  280.    {
  281.       this.ground = _global.MallCrawl["GLevel" + this.glevel]._y - this._height + this.HEIGHT_MINUS;
  282.       this._y = this.ground;
  283.    }
  284.    function jump()
  285.    {
  286.       this.jump_is_available = false;
  287.       this.accel = -1 * this.JUMP_SPEED;
  288.       this._y = this.ground - 1;
  289.    }
  290.    function rideEscalator(escalator, type)
  291.    {
  292.       this._x = escalator._x;
  293.       if(type == "up")
  294.       {
  295.          if(this._y > escalator._y && this.last_escalator != escalator)
  296.          {
  297.             this.is_available = false;
  298.             this.last_escalator = escalator;
  299.             escalator.activate(this);
  300.          }
  301.       }
  302.       else if(type == "down")
  303.       {
  304.          if(this._y < escalator._y - 5 && this.last_escalator != escalator)
  305.          {
  306.             this.is_available = false;
  307.             this.last_escalator = escalator;
  308.             escalator.activate(this);
  309.          }
  310.       }
  311.    }
  312.    function moveLeft(speed)
  313.    {
  314.       !!speed ? null : (speed = this.move_speed);
  315.       this._xscale >= 0 ? null : (this._xscale *= -1);
  316.       if(this._x > this._width)
  317.       {
  318.          this._x -= speed;
  319.       }
  320.       if(this._x + _global.MallCrawl._x < this.WIDTH_BOUNDS.left)
  321.       {
  322.          if(_global.MallCrawl._x < 0)
  323.          {
  324.             _global.MallCrawl._x += speed;
  325.          }
  326.          else
  327.          {
  328.             _global.MallCrawl._x = 0;
  329.          }
  330.       }
  331.    }
  332.    function moveRight(speed)
  333.    {
  334.       !!speed ? null : (speed = this.move_speed);
  335.       this._xscale <= 0 ? null : (this._xscale *= -1);
  336.       if(this._x < this.BACKGROUND_WIDTH - this._width)
  337.       {
  338.          this._x += speed;
  339.       }
  340.       if(this._x + _global.MallCrawl._x > this.WIDTH_BOUNDS.right)
  341.       {
  342.          if(_global.MallCrawl._x > - (this.BACKGROUND_WIDTH - this.STAGE_WIDTH))
  343.          {
  344.             _global.MallCrawl._x -= speed;
  345.          }
  346.          else
  347.          {
  348.             _global.MallCrawl._x = - (this.BACKGROUND_WIDTH - this.STAGE_WIDTH);
  349.          }
  350.       }
  351.    }
  352.    function resetPowerItems()
  353.    {
  354.       this.Wheeler.gotoAndStop("stop");
  355.       this.Trike.gotoAndStop("stop");
  356.       this.Guitar.gotoAndStop("stop");
  357.       this.SkateBoard.gotoAndStop("stop");
  358.    }
  359. }
  360.